{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d9772664",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Phone number found: 415-555-4242\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "phoneNumRegex = re.compile(r'\\d{3}-\\d{3}-\\d{4}')\n",
    "mo = phoneNumRegex.search('My number is 415-555-4242')\n",
    "print('Phone number found: ' + mo.group())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a2e2918c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "415\n",
      "555-4242\n",
      "415-555-4242\n",
      "415-555-4242\n",
      "('415', '555-4242')\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "('415', '555-4242')"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "phoneNumRegex = re.compile(r'(\\d{3})-(\\d{3}-\\d{4})')\n",
    "mo = phoneNumRegex.search('My number is 415-555-4242')\n",
    "print(mo.group(1))\n",
    "print(mo.group(2))\n",
    "print(mo.group(0))\n",
    "print(mo.group())\n",
    "print(mo.groups())\n",
    "areaCode, mainNumber = mo.groups()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "542decf1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "415-555-4242\n",
      "555-4242\n"
     ]
    }
   ],
   "source": [
    "phoneNumRegex = re.compile(r'(\\d{3}-)?\\d{3}-\\d{4}')\n",
    "mo1 = phoneNumRegex.search('My number is 415-555-4242')\n",
    "print(mo1.group())\n",
    "\n",
    "mo2 = phoneNumRegex.search('My number is 555-4242')\n",
    "print(mo2.group())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fdd16f22",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "HaHaHaHaHa\n",
      "HaHaHa\n"
     ]
    }
   ],
   "source": [
    "# 贪婪模式(默认)\n",
    "greedyHaRegex = re.compile(r'(Ha){3,5}')\n",
    "mo1 = greedyHaRegex.search('HaHaHaHaHa')\n",
    "print(mo1.group())\n",
    "\n",
    "# 非贪婪模式\n",
    "nonegreedyHaRegex = re.compile(r'(Ha){3,5}?')\n",
    "mo2 = nonegreedyHaRegex.search('HaHaHaHaHa')\n",
    "print(mo2.group())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "07fd07f7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['415-555-4242', '415-555-0000']"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "phoneNumRegex = re.compile(r'\\d{3}-\\d{3}-\\d{4}') # has no groups\n",
    "phoneNumRegex.findall(\"Cell 415-555-4242 Work: 415-555-0000\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "f07da6ce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('415', '555', '4242'), ('415', '555', '0000')]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "phoneNumRegex = re.compile(r'(\\d{3})-(\\d{3})-(\\d{4})') # has no groups\n",
    "phoneNumRegex.findall(\"Cell 415-555-4242 Work: 415-555-0000\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "1bf4b18e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['12 drnummers',\n",
       " '11 pipers',\n",
       " '10 lords',\n",
       " '9 ladies',\n",
       " '8 maids',\n",
       " '7 swans',\n",
       " '6 geese',\n",
       " '5 rings',\n",
       " '4 birds',\n",
       " '3 hens',\n",
       " '2 doves',\n",
       " '1 partridge']"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xmasRegex = re.compile(r'\\d+\\s\\w+')\n",
    "xmasRegex.findall('12 drnummers, 11 pipers, 10 lords, 9 ladies, 8 maids, 7 swans, 6 geese, 5 rings, 4 birds, 3 hens, 2 doves, 1 partridge')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "06c20283",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['R',\n",
       " 'b',\n",
       " 'C',\n",
       " 'p',\n",
       " ' ',\n",
       " 't',\n",
       " 's',\n",
       " ' ',\n",
       " 'b',\n",
       " 'b',\n",
       " 'y',\n",
       " ' ',\n",
       " 'f',\n",
       " 'd',\n",
       " '.',\n",
       " ' ',\n",
       " 'B',\n",
       " 'B',\n",
       " 'Y',\n",
       " ' ',\n",
       " 'F',\n",
       " 'D',\n",
       " '.']"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowelRegex = re.compile(r'[^aeiouAEIOU]')\n",
    "vowelRegex.findall('RoboCop eats baby food. BABY FOOD.')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "1f9dea03",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['cat', 'hat', 'sat', 'lat', 'mat']"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "atRegex = re.compile(r'.at')\n",
    "atRegex.findall('The cat in the hat sat on the flat mat.')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
